<script lang="ts" setup>
import { reactive, ref } from "vue"
import { useRouter } from "vue-router"
import { useUserStore } from "@/store/modules/user"
import { type FormInstance, type FormRules } from "element-plus"
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
import { getLoginCodeApi } from "@/api/login"
import { type LoginRequestData } from "@/api/login/types/login"
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
import Owl from "./components/Owl.vue"
import { useFocus } from "./hooks/useFocus"

const router = useRouter()
const { handleBlur, handleFocus } = useFocus()
const loginFormRef = ref<FormInstance | null>(null)
const loading = ref(false)
const codeUrl = ref<any>("")
const loginFormData: LoginRequestData = reactive({
  username: localStorage.getItem('username') || "",
  password: localStorage.getItem('password') || "",
  code: ""
})

// 13265906694
// 13265906616
// 13265906696
const rember = ref<boolean>(localStorage.getItem('username')!=null&&localStorage.getItem('password')!=null)
// console.log(localStorage.getItem('username'))
const loginFormRules: FormRules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
  ],
  code: [{ required: true, message: "请输入验证码", trigger: "blur" }]
}

const handleLogin = () => {
  loginFormRef.value?.validate((valid: boolean, fields) => {
    if (!valid) return
    loading.value = true
    useUserStore().login(loginFormData).then(() => {
      if(rember.value) {
        localStorage.setItem('username', loginFormData.username)
        localStorage.setItem('password', loginFormData.password)
      } else {
        localStorage.removeItem('username')
        localStorage.removeItem('password')
      }
      router.push({ path: "/vehicle/vehiclemanage" })
    }).catch(() => {
      createCode()
      loginFormData.password = ""
      loading.value = false
    })
  })
}
const createCode = () => {
  loginFormData.code = ""
  codeUrl.value = getLoginCodeApi()
}
const toEdit = () => {
  router.push({path: '/editPassword'})
}
createCode()
</script>

<template>
  <div class="login-container">
    <ThemeSwitch class="theme-switch" />
    <div class="login-card">
      <div class="title">
        <img src="@/assets/images/logo.jpg" />
      </div>
      <!-- <div class="fir-title">得新清洁管理后台</div> -->
      <div class="sub-title">登录</div>
      <div class="content">
        <el-form ref="loginFormRef" :model="loginFormData" :rules="loginFormRules" @keyup.enter="handleLogin">
          <el-form-item prop="username">
            <el-input
              v-model.trim="loginFormData.username"
              placeholder="用户名"
              type="text"
              tabindex="1"
              :prefix-icon="User"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model.trim="loginFormData.password"
              placeholder="密码"
              type="password"
              tabindex="2"
              :prefix-icon="Lock"
              show-password
              @blur="handleBlur"
              @focus="handleFocus"
            />
          </el-form-item>
          <!-- <el-form-item prop="code">
            <el-input
              v-model.trim="loginFormData.code"
              placeholder="验证码"
              type="text"
              tabindex="3"
              :prefix-icon="Key"
              maxlength="5"
            >
              <template #append>
                <el-image  :src="codeUrl" @click="createCode" draggable="false">
                  <template #placeholder>
                    <el-icon>
                      <Picture />
                    </el-icon>
                  </template>
                  <template #error>
                    <el-icon>
                      <Loading />
                    </el-icon>
                  </template>
                </el-image>
              </template>
            </el-input>
          </el-form-item> -->

          <div class="handle">
            <el-checkbox v-model="rember" label="记住密码" size="large" />
            <span class="editPassword" @click="toEdit">修改密码</span>
          </div>
          <el-button :loading="loading" type="primary" @click.prevent="handleLogin">登 录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.handle{
  display: flex;
  justify-content: space-between;
  align-items: center;
  .editPassword{
    cursor: pointer;
    font-size:12px;
    color:var(--el-color-primary);
  }
  :deep(.el-checkbox__label){
    font-size:12px;
  }
}
.login-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
  min-height: 100%;
  .theme-switch {
    position: fixed;
    top: 5%;
    right: 5%;
    cursor: pointer;
  }
  .login-card {
    width: 428px;
    border-radius: 20px;
    box-shadow: 0 0 10px #dcdfe6;
    background-color: var(--el-bg-color);
    overflow: hidden;
    .fir-title{
      font-size:16px;
      text-align: center;
    }
    .sub-title{
      font-size:20px;
      font-weight: 600;
      text-align: center;
      margin-top:15px;
    }
    .title {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-top:25px;
      img {
        width: 100px;
      }
    }
    .content {
      padding: 20px 50px 50px 50px;
      :deep(.el-input-group__append) {
        padding: 0;
        overflow: hidden;
        .el-image {
          width: 100px;
          height: 32px;
          border-left: 0px;
          user-select: none;
          cursor: pointer;
          text-align: center;
        }
      }
      .el-button {
        width: 100%;
        margin-top: 10px;
      }
    }
  }
}
</style>
